package com.lmk.qs.jdbc;

import com.lmk.qs.domain.Emp;
import com.lmk.qs.utils.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class test04 {
    public static void main(String[] args) {
        List<Emp> list = new test04().findAll3();
        System.out.println(list);
    }

    /**
     * \
     *
     * @return
     */
    public List<Emp> findALL() {
        ResultSet resultSet = null;
        Connection connection = null;
        Statement st = null;
        List<Emp> list = new ArrayList<Emp>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kk", "root", "root");
            String sql = "select * from emp";
            st = connection.createStatement();
            resultSet = st.executeQuery(sql);
            Emp emp = null;

            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String gender = resultSet.getString(3);
                int salary = resultSet.getInt(4);
                int jobdate = resultSet.getInt(5);
                int dep_id = resultSet.getInt(6);
                emp = new Emp();
                emp.setId(id);
                emp.setName(name);
                emp.setGender(gender);
                emp.setSalary(salary);
                emp.setJob_date(jobdate);
                emp.setDep_id(dep_id);
                list.add(emp);

            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }


    public List<Emp> findALL2() {
        ResultSet resultSet = null;
        Connection connection = null;
        Statement st = null;
        List<Emp> list = new ArrayList<Emp>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from emp";
            st = connection.createStatement();
            resultSet = st.executeQuery(sql);
            Emp emp = null;

            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String gender = resultSet.getString(3);
                int salary = resultSet.getInt(4);
                int jobdate = resultSet.getInt(5);
                int dep_id = resultSet.getInt(6);
                emp = new Emp();
                emp.setId(id);
                emp.setName(name);
                emp.setGender(gender);
                emp.setSalary(salary);
                emp.setJob_date(jobdate);
                emp.setDep_id(dep_id);
                list.add(emp);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(resultSet, st, connection);
        }
        return list;
    }

    public List<Emp> findAll3() {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        List<Emp> list = new ArrayList<Emp>();

        try {
            Emp emp = null;
            con = JDBCUtil.getConnection();
            String sql = "select * from emp";
            st = con.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String gender = rs.getString(3);
                int salary = rs.getInt(4);
                int jobdate = rs.getInt(5);
                int dep_id = rs.getInt(6);
                emp = new Emp();
                emp.setId(id);
                emp.setName(name);
                emp.setGender(gender);
                emp.setSalary(salary);
                emp.setJob_date(jobdate);
                emp.setDep_id(dep_id);
                list.add(emp);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(rs, st, con);
        }

        return list;

    }

}
